package com.cgtong.android.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;
import com.cgtong.android.App;
import com.cgtong.android.R;
import com.cgtong.android.activity.ProblemDetailActivity;
import com.cgtong.android.bean.ProblemBean;
import com.cgtong.android.util.CommonUtils;
import com.cgtong.android.widget.GlideApp;
import com.cgtong.android.db.UserAccount;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.cgtong.android.callback.DiffCallBack.BUNDLE_IS_USER_ATTENTION;

/**
 * 列表中带有水平处理进度
 * Created by chunc on 2017/10/18.
 * BrowseProblemsActivity
 * HisProblemsActivity
 * TimeLimitActivity
 */

public class ProblemsAdapter extends BaseQuickAdapter<ProblemBean, BaseViewHolder> {

    private boolean isShowFollowIcon = true;
    private Context context;
    private long mCurUserId;

    public void setShowFollowIcon(boolean isShowFollowIcon) {
        this.isShowFollowIcon = isShowFollowIcon;
    }


    public void setAllNewData(
            ArrayList<ProblemBean> problemBeanList) {
        mData.clear();
        mData.addAll(problemBeanList);
    }


    public ProblemsAdapter(@Nullable List<ProblemBean> data, Context context) {
        super(R.layout.adapter_browse_problem, data);
        this.context = context;
        UserAccount userAccount = App.getInstance().getCurrentAccount();
        if (null != userAccount)
            this.mCurUserId = userAccount.getUserId();
    }

    @Override
    protected void convert(BaseViewHolder helper, ProblemBean item) {
        ImageView avatarImg = helper.getView(R.id.avatar_img);
        if (item.getAvasterId() >= 1 && item.getAvasterId() <= 12) {
            CommonUtils.setUserAvatar(mContext, avatarImg, item.getAvasterId());
        } else {
            LogUtils.d("avatarId错误");
            avatarImg.setImageResource(R.drawable.head_1);
        }

        //如昵称和手机号相同，隐藏手机号中间5位
//        if (RegexUtils.isMobileSimple(item.getNickName())) {
//            String nikeName = item.getNickName().replaceAll("(\\d{3})\\d{5}(\\d{3})", "$1*****$2");
//            helper.setText(R.id.name_txt, nikeName);
//        } else {
//            helper.setText(R.id.name_txt, item.getNickName());
//        }
        helper.setText(R.id.name_txt, item.getNickName());
        helper.setText(R.id.date_txt, item.getCreateTime());
        helper.setText(R.id.location_txt, item.getAddress());

        if (isShowFollowIcon) {
            helper.addOnClickListener(R.id.follow_fl);
            ImageView followImg = helper.getView(R.id.follow_img);
//            if (mCurUserId == item.getCreateUserId()) {
//                followImg.setVisibility(View.GONE);
//            } else {
//                followImg.setVisibility(View.VISIBLE);
            //是否关注了用户
            if (item.getIsCreateUserAttention() == 1) {
                //已关注
                followImg.setBackgroundDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_followed));
            } else {
                //未关注
                followImg.setBackgroundDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_to_follow));
            }
//            }
        } else {
            helper.setGone(R.id.follow_img, false);
        }


        //展示图片
        List<ProblemBean.ResourceBean> picList = item.getResourceList();
        if (picList != null && picList.size() > 0) {
            helper.setGone(R.id.grid_view, true);
            GridView gridView = helper.getView(R.id.grid_view);
            gridView.setAdapter(new ProblemPictureAdapter(picList, item));
        } else {
            helper.setGone(R.id.grid_view, false);
        }

        //处理进度
        RecyclerView stepRecyclerView = helper.getView(R.id.step_recycler_view);
        stepRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        stepRecyclerView.setAdapter(new HorizontalStepAdapter(mContext, item.getStatus(), item.getIsExpired(), item.getStatusChangeList()));

        //录音
        if (!TextUtils.isEmpty(item.getVoiceUrl()) && item.getVoiceUrl().startsWith("http")) {
            helper.setGone(R.id.audio_ll, true);
            helper.setText(R.id.play_audio_duration_txt,item.getVoiceTime()+ "\"");
        } else {
            helper.setGone(R.id.audio_ll, false);
        }
        //文字
        if (!TextUtils.isEmpty(item.getDesc())) {
            helper.setGone(R.id.content_txt, true);
            helper.setText(R.id.content_txt, item.getDesc());
        } else {
            helper.setGone(R.id.content_txt, false);
        }
    }


    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position, List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else {
            Bundle bundle = (Bundle) payloads.get(0);
            for (String key : bundle.keySet()) {
                switch (key) {
                    case BUNDLE_IS_USER_ATTENTION:
                        int isFollow = bundle.getInt(key);
                        LogUtils.d("onBindViewHolder isFollow " + isFollow);
                        //是否关注了用户
                        ImageView followImg = holder.getView(R.id.follow_img);
                        if (isFollow == 1) {
                            //已关注
                            followImg.setBackgroundDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_followed));
                        } else {
                            //未关注
                            followImg.setBackgroundDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_to_follow));
                        }
                        break;
                }
            }

        }
    }


    class ProblemPictureAdapter extends BaseAdapter {

        private List<ProblemBean.ResourceBean> mList;
        private ProblemBean mProblemBean;

        public ProblemPictureAdapter(List<ProblemBean.ResourceBean> mList, ProblemBean problemBean) {
            this.mList = mList;
            this.mProblemBean = problemBean;
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public ProblemBean.ResourceBean getItem(int position) {
            if (position <= mList.size() - 1) {
                return mList.get(position);
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mLayoutInflater.inflate(R.layout.layout_picture, parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            ProblemBean.ResourceBean item = getItem(position);
            if (item != null) {
                GlideApp.with(context).load(getItem(position).getSmallContent()).dontAnimate()
                        .placeholder(R.drawable.icon_default_loading)
                        .error(R.drawable.icon_default_error).into(holder.imageView);

                holder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        ImagePreviewActivity.start(mContext, mList, position);
                        boolean canDelete = false;
                        boolean canEvaluate = false;
                        if (App.getInstance().isUserLogin()) {
                            if ((mProblemBean.getStatus() == 4 || mProblemBean.getStatus() == 5 || mProblemBean.getStatus() == 7) && App.getInstance().isOwnself(mProblemBean.getCreateUserId())) {
                                canDelete = true;
                            }
                            if ((mProblemBean.getStatus() == 4 || mProblemBean.getStatus() == 5) && App.getInstance().isOwnself(mProblemBean.getCreateUserId())) {
                                canEvaluate = true;
                            }
                        }
                        ProblemDetailActivity.start(mContext, mProblemBean.getId(), canDelete, canEvaluate);
                    }
                });
            } else {
                holder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean canDelete = false;
                        boolean canEvaluate = false;
                        if (App.getInstance().isUserLogin()) {
                            if ((mProblemBean.getStatus() == 4 || mProblemBean.getStatus() == 5 || mProblemBean.getStatus() == 7) && App.getInstance().isOwnself(mProblemBean.getCreateUserId())) {
                                canDelete = true;
                            }
                            if ((mProblemBean.getStatus() == 4 || mProblemBean.getStatus() == 5) && App.getInstance().isOwnself(mProblemBean.getCreateUserId())) {
                                canEvaluate = true;
                            }
                        }
                        ProblemDetailActivity.start(mContext, mProblemBean.getId(), canDelete, canEvaluate);
                    }
                });
            }
            return convertView;
        }


        class ViewHolder {

            @BindView(R.id.img_view)
            ImageView imageView;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
